﻿using BCCommon;
using BCEntity.TMS.MarketingCoupon.MarketingCouponUsing;
using Dapper;
using System.Collections.Generic;
using System.Data;
using System.Threading.Tasks;

namespace BCData.TMS.MarketingCoupon.MarketingCouponUsing
{
    public class MarketingCouponUsingData : IMarketingCouponUsingData
    {
        private const string QUERY_EXISTS = @"SELECT 1 FORM marketingcouponusing";
        private const string SQL_QUERY = @"SELECT
		                                        A.MarketingCouponUsingId,
		                                        A.UserId,
		                                        A.UserName,
		                                        A.MarketingCouponTypeId,
		                                        A.CouponType,
		                                        A.MarketingCouponId,
		                                        A.CouponNumber,
		                                        A.CouponName,
		                                        A.CouponSummark,
		                                        A.AgentRecordId,
		                                        A.IsCommon,
		                                        A.ProvinceCode,
		                                        A.CityCode,
		                                        A.CountyCode,
		                                        A.StartDate,
		                                        A.EndDate,
		                                        A.CouponAmount,
		                                        A.CreateTime,
		                                        A.OrderId,
		                                        A.OrderNo,
		                                        A.IsCommonBusiness,
		                                        A.VehicleTransportBusinessConfigId,
		                                        A.TransportBusiness
                                            FROM
                                                marketingcouponusing A  ";
        private readonly IDatabaseContext database;
        public MarketingCouponUsingData(IDatabaseContext database)
        {
            this.database = database;
        }

        public async Task<bool> Exists(long marketingCouponId)
        {
            string sql = string.Format("{0} WHERE MarketingCouponId=?MarketingCouponId LIMIT 1;", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?MarketingCouponId", marketingCouponId, DbType.Int64);
            return await this.database.Slave.ExecuteScalarAsync(sql, parameters)!=null;
        }

        public async Task<MarketingCouponUsingEntity> GetSingle(long marketingCouponUsingId)
        {
            var sql = typeof(MarketingCouponUsingEntity).GetSelectSqlByPrimaryKeys();
            return await this.database.Slave.QueryFirstOrDefaultAsync<MarketingCouponUsingEntity>(sql, new { marketingCouponUsingId });
        }

        public async Task<MarketingCouponUsingEntity> GetSingleByCouponId(long marketingCouponId)
        {
            var sql = $" {SQL_QUERY} WHERE A.MarketingCouponId=?MarketingCouponId ";
            return await this.database.Slave.QueryFirstOrDefaultAsync<MarketingCouponUsingEntity>(sql, new { marketingCouponId });
        }

        public async Task<MarketingCouponUsingEntity> InsertAsync(MarketingCouponUsingEntity entity)
        {
            return await this.database.Master.InsertAsync(entity, database.Transaction);
        }

        public async Task<(IEnumerable<MarketingCouponUsingEntity>, int)> Query(
            long? marketingCouponTypeId, long? userId, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            if (marketingCouponTypeId.HasValue)
            {
                wheres.Add("A.MarketingCouponTypeId = ?MarketingCouponTypeId");
                parameters.Add("?MarketingCouponTypeId", marketingCouponTypeId.Value, DbType.Int64);
            }
            if (userId.HasValue)
            {
                wheres.Add("A.UserId = ?UserId");
                parameters.Add("?UserId", userId.Value, DbType.Int64);
            }
            string whereSql = string.Empty;
            if (wheres.Count > 0)
            {
                whereSql = $"WHERE {string.Join(" AND ", wheres)}";
            }

            var sqlCount = " SELECT COUNT(A.MarketingCouponUsingId) FROM marketingcouponusing A ";
            string sql = $@"{SQL_QUERY} {whereSql} Order By A.MarketingCouponUsingId DESC 
                        LIMIT {(pageIndex - 1) * pageSize}, {pageSize};{sqlCount}{whereSql} ";
            return await this.database.Slave.QueryPageableListAsync<MarketingCouponUsingEntity>(sql, parameters);
        }

        public async Task<MarketingCouponUsingEntity> UpdateAsync(MarketingCouponUsingEntity entity)
        {
            return await this.database.Master.UpdateAsync(entity, database.Transaction);
        }
    }
}
